//
//  ReactionComputer.m
//  MacHeliosSim
//
//  Created by Paul Thompson on 8/11/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Reaction.h"


@implementation Reaction

-(id)initWithBeamEnergy:(float)theBeamEnergy
			AndBeamMass:(int)theBeamMass
		  AndBeamSymbol:(NSString *)theBeamSymbol
		  AndTargetMass:(int)theTargetMass
		AndTargetSymbol:(NSString *)theTargetSymbol
		AndEjectileMass:(int)theEjectileMass
	  AndEjectileSymbol:(NSString *)theEjectileSymbol
		AndNuclideTable:(NuclideTable *)theTable
{
	currentTable = theTable;
	beamEnergy = theBeamEnergy;
	beamNuclide = [currentTable entryForNucleonCount:theBeamMass
								   ElementSymbol:theBeamSymbol];
	
	
	targetNuclide = [currentTable entryForNucleonCount:theTargetMass
									 ElementSymbol:theTargetSymbol];
			
	ejectileNuclide = [currentTable entryForNucleonCount:theEjectileMass
									   ElementSymbol:theEjectileSymbol];
	
	int productNucleonCount = [beamNuclide nucleonCount] + [targetNuclide nucleonCount] - [ejectileNuclide nucleonCount];
	int productProtonCount = [beamNuclide protonCount] + [targetNuclide protonCount] - [ejectileNuclide protonCount];
	productNuclide = [currentTable entryForNucleonCount:productNucleonCount ProtonCount:productProtonCount];
	[self calcQValue];
	return self;
}

-(id)initWithBeamEnergy:(float)energy
			   AndNuclide:(Nuclide *)beam
		   TargetNuclide:(Nuclide *)target
		 EjectileNuclide:(Nuclide *)ejectile
		 AndNuclideTable:(NuclideTable *)theTable
{
	beamEnergy = energy;
	beamNuclide = beam;	
	targetNuclide = target;
	ejectileNuclide = ejectile;
	currentTable = theTable;
	[self calcQValue];
	return nil;
}

-(id)calculateReaction
{
	
	// BEGIN ACTUAL COMPUTATION  
	//  Edit: Of what?
	
	//	Recalculates the product and Q value... not sure why I'd ever want to do this, since
	//	it implies that some of the nuclides got changed somehow, but oh well.
	
	int productNucleonCount = [beamNuclide nucleonCount] + [targetNuclide nucleonCount] - [ejectileNuclide nucleonCount];
	int productProtonCount = [beamNuclide protonCount] + [targetNuclide protonCount] - [ejectileNuclide protonCount];
	productNuclide = [currentTable entryForNucleonCount:productNucleonCount ProtonCount:productProtonCount];
	[self calcQValue];
	
	return self;
}

-(float)calcQValue
{
	//	this method calculates the qValue for the reaction and both sets the member variable, AND
	//	returns it, so it's a bit redundant.  to get the Q value you can either call 
	//	[self calcQValue]; then access qValue directly, or you can take the return from the call.
	
	
	float uConvert = 931.494028*1000.0;// mass excesses are in keV
	qValue = (([beamNuclide nucleonCount]*uConvert+[beamNuclide deltaM]) 
					+([targetNuclide nucleonCount]*uConvert+[targetNuclide deltaM])
					-([ejectileNuclide nucleonCount]*uConvert+[ejectileNuclide deltaM])
					-([productNuclide nucleonCount]*uConvert+[productNuclide deltaM]))/1000.0;
	
	
	return qValue;
}

@synthesize currentTable, beamNuclide, targetNuclide, ejectileNuclide, productNuclide,
			qValue, beamEnergy;

@end
